
#include "RtBase.h"
#include "RtReactorThreadProxy.h"

CRtReactorThreadProxy::CRtReactorThreadProxy(IRtReactor *aReactor, 
											 CRtThreadManager::TType aType)
	: IRtReactor(NULL_PROPERTY)
	, CRtThreadProxyBase(aType)
	, m_pReactor(aReactor)
{
	RT_ASSERTE(m_pReactor);
}

CRtReactorThreadProxy::~CRtReactorThreadProxy()
{
}

RtResult CRtReactorThreadProxy::Open()
{
	RT_ASSERTE(!"CRtReactorThreadProxy::Open");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::
RegisterHandler(IRtEventHandler *aEh, IRtEventHandler::MASK aMask)
{
	CEventRegisterHandler *pEvent = new CEventRegisterHandler(m_pReactor, aEh, aMask);
	return SendEvent_i(pEvent);
}

RtResult CRtReactorThreadProxy::
RemoveHandler(IRtEventHandler *aEh, IRtEventHandler::MASK aMask)
{
	RT_INFO_TRACE("CRtReactorThreadProxy::RemoveHandler, begin,"
		" aEh=" << aEh << " aMask=" <<aMask);

	CEventRemoveHandler *pEvent = new CEventRemoveHandler(m_pReactor, aEh, aMask);
	RtResult rv = SendEvent_i(pEvent);

	RT_INFO_TRACE("CRtReactorThreadProxy::RemoveHandler, end.");
	return rv;
}

RtResult CRtReactorThreadProxy::
NotifyHandler(IRtEventHandler *aEh, IRtEventHandler::MASK aMask)
{
	RT_ASSERTE(!"CRtReactorThreadProxy::NotifyHandler");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::RunEventLoop()
{
	RT_ASSERTE(!"CRtReactorThreadProxy::RunEventLoop");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::StopEventLoop()
{
	RT_ASSERTE(!"CRtReactorThreadProxy::StopEventLoop");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::Close()
{
	RT_ASSERTE(!"CRtReactorThreadProxy::Close");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::
ScheduleTimer(IRtTimerHandler *aTh, LPVOID aArg,
			  const CRtTimeValue &aInterval, DWORD aCount)
{
	RT_ASSERTE(!"CRtReactorThreadProxy::ScheduleTimer");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::CancelTimer(IRtTimerHandler *aTh)
{
	RT_ASSERTE(!"CRtReactorThreadProxy::CancelTimer");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::PostEvent(IRtEvent* aEvent)
{
	RT_ASSERTE(!"CRtReactorThreadProxy::PostEvent");
	return RT_ERROR_NOT_IMPLEMENTED;
}

RtResult CRtReactorThreadProxy::SendEvent(IRtEvent* aEvent)
{
	RT_ASSERTE(!"CRtReactorThreadProxy::SendEvent");
	return RT_ERROR_NOT_IMPLEMENTED;
}


//////////////////////////////////////////////////////////////////////
// class CRtReactorThreadProxy
//////////////////////////////////////////////////////////////////////

CRtReactorThreadProxy::CEventRegisterHandler::~CEventRegisterHandler()
{
}

RtResult CRtReactorThreadProxy::CEventRegisterHandler::OnEventFire()
{
	RtResult rv = RT_ERROR_NULL_POINTER;
	if (m_pReactor) {
		rv = m_pReactor->RegisterHandler(m_pEh, m_Mask);
		if (RT_FAILED(rv)) {
			RT_ERROR_TRACE("CRtReactorThreadProxy::CEventRegisterHandler::OnEventFire,"
				" m_pReactor->RegisterHandler() failed!"
				" m_pEh=" << m_pEh << 
				" m_Mask=" << m_Mask << 
				" rv=" << rv);
		}
	}
	return rv;
}


//////////////////////////////////////////////////////////////////////
// class CEventRemoveHandler
//////////////////////////////////////////////////////////////////////

CRtReactorThreadProxy::CEventRemoveHandler::~CEventRemoveHandler()
{
}

RtResult CRtReactorThreadProxy::CEventRemoveHandler::OnEventFire()
{
	RtResult rv = RT_ERROR_NULL_POINTER;
	if (m_pReactor) {
		rv = m_pReactor->RemoveHandler(m_pEh, m_Mask);
		if (RT_FAILED(rv)) {
			RT_ERROR_TRACE("CRtReactorThreadProxy::CEventRegisterHandler::OnEventFire,"
				" m_pReactor->RemoveHandler() failed!"
				" m_pEh=" << m_pEh << 
				" m_Mask=" << m_Mask << 
				" rv=" << rv);
		}
	}
	RT_INFO_TRACE("CRtReactorThreadProxy::CEventRegisterHandler::OnEventFire, rv=" << rv);
	return rv;
}
